2 * Copyright 2013 Canonical Ltd.
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.
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.
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/>.
17 * Michael Zanetti <michael.zanetti@canonical.com>
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"
29#include <QDBusInterface>
31#include <QDomDocument>
33// This is a mock, specifically to test the LauncherModel
34class MockApp: public unity::shell::application::ApplicationInfoInterface
38 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
39 QString appId() const { return m_appId; }
40 QString name() const { return "mock"; }
41 QString comment() const { return "this is a mock"; }
42 QUrl icon() const { return QUrl(); }
43 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
44 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
45 bool focused() const { return m_focused; }
46 QUrl screenshot() const { return QUrl(); }
48 // Methods used for mocking (not in the interface)
49 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
55// This is a mock, specifically to test the LauncherModel
56class MockAppManager: public unity::shell::application::ApplicationManagerInterface
60 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
61 int rowCount(const QModelIndex &) const { return m_list.count(); }
62 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
63 QString focusedApplicationId() const {
64 Q_FOREACH(MockApp *app, m_list) {
65 if (app->focused()) return app->appId();
69 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
70 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
71 Q_FOREACH(MockApp* app, m_list) {
72 if (app->appId() == appId) {
78 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
79 bool stopApplication(const QString &) { return false; }
80 bool focusApplication(const QString &appId) {
81 Q_FOREACH(MockApp* app, m_list) {
82 app->setFocused(app->appId() == appId);
84 Q_EMIT focusedApplicationIdChanged();
88 void unfocusCurrentApplication() { }
90 void addApplication(MockApp *app) {
91 beginInsertRows(QModelIndex(), count(), count());
95 void removeApplication(int index) {
96 beginRemoveRows(QModelIndex(), index, index);
97 m_list.takeAt(index)->deleteLater();
100 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
101 bool suspended() const { return false; }
102 void setSuspended(bool) {}
103 bool dashActive() const { return false; }
104 void setDashActive(bool) {}
107 QList<MockApp*> m_list;
110class LauncherModelTest : public QObject
115 LauncherModel *launcherModel;
116 MockAppManager *appManager;
120 void initTestCase() {
121 launcherModel = new LauncherModel(this);
122 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
124 appManager = new MockAppManager(this);
125 launcherModel->setApplicationManager(appManager);
128 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
130 appManager->addApplication(new MockApp("abs-icon"));
131 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
133 appManager->addApplication(new MockApp("no-icon"));
134 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
137 // Removing apps from appmanager and launcher as pinned ones would stick
139 while (appManager->count() > 0) {
140 appManager->removeApplication(0);
142 while (launcherModel->rowCount(QModelIndex()) > 0) {
143 launcherModel->requestRemove(launcherModel->get(0)->appId());
148 QCOMPARE(launcherModel->get(0)->pinned(), false);
149 QCOMPARE(launcherModel->get(1)->pinned(), false);
151 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
152 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
153 launcherModel->move(1, 0);
155 QCOMPARE(item0BeforeMove, launcherModel->get(1));
156 QCOMPARE(item1BeforeMove, launcherModel->get(0));
158 // moved item must be pinned now
159 QCOMPARE(item0BeforeMove->pinned(), false);
160 QCOMPARE(item1BeforeMove->pinned(), true);
164 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
165 QCOMPARE(launcherModel->get(0)->pinned(), false);
166 QCOMPARE(launcherModel->get(1)->pinned(), false);
167 launcherModel->pin(launcherModel->get(0)->appId());
168 QCOMPARE(launcherModel->get(0)->pinned(), true);
169 QCOMPARE(launcherModel->get(1)->pinned(), false);
170 QCOMPARE(spy.count(), 1);
171 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
173 launcherModel->requestRemove(launcherModel->get(0)->appId());
174 QCOMPARE(launcherModel->get(0)->pinned(), false);
175 QCOMPARE(launcherModel->get(1)->pinned(), false);
176 QCOMPARE(spy.count(), 2);
177 QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
180 void testRemove_data() {
182 QTest::addColumn<bool>("pinned");
183 QTest::addColumn<bool>("running");
185 QTest::newRow("non-pinned, running") << false << true;
186 QTest::newRow("pinned, running") << true << false;
187 QTest::newRow("pinned, non-running") << true << false;
191 QFETCH(bool, pinned);
192 QFETCH(bool, running);
194 // In the beginning we always have two items
195 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
197 // pin one if required
199 launcherModel->pin(launcherModel->get(1)->appId());
202 // stop it if required
204 appManager->removeApplication(1);
208 launcherModel->requestRemove(launcherModel->get(1)->appId());
211 // both apps are running, both apps must still be here
212 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
214 // Item must be unpinned now
215 QCOMPARE(launcherModel->get(1)->pinned(), false);
218 // Item 1 must go away, item 0 is here to stay
219 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
222 // done our checks. now stop the app if was still running
224 appManager->removeApplication(1);
227 // It needs to go away in any case now
228 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
231 void testQuickListPinningRemoving() {
232 // we start with 2 unpinned items
233 QCOMPARE(launcherModel->get(0)->pinned(), false);
234 QCOMPARE(launcherModel->get(1)->pinned(), false);
235 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
237 // find the Pin item in the quicklist
238 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
239 int pinActionIndex = -1;
240 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
241 if (model->get(i).actionId() == "pin_item") {
246 QVERIFY(pinActionIndex >= 0);
248 // trigger pin item quicklist action => Item must be pinned now.
249 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
250 QCOMPARE(launcherModel->get(0)->pinned(), true);
251 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
253 // quicklist needs to transform to remove item. trigger it and check it item goes away
254 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
255 QCOMPARE(launcherModel->get(0)->pinned(), false);
257 // still needs to be here as the app is still here
258 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
260 appManager->removeApplication(0);
261 // Now it needs to go away
262 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
265 void testApplicationFocused() {
266 // all apps unfocused at beginning...
267 QCOMPARE(launcherModel->get(0)->focused(), false);
268 QCOMPARE(launcherModel->get(1)->focused(), false);
270 appManager->focusApplication("abs-icon");
271 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
272 QCOMPARE(launcherModel->get(0)->focused(), true);
273 QCOMPARE(launcherModel->get(1)->focused(), false);
275 appManager->focusApplication("no-icon");
276 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
277 QCOMPARE(launcherModel->get(0)->focused(), false);
278 QCOMPARE(launcherModel->get(1)->focused(), true);
281 void testClosingApps() {
282 // At the start there are 2 items. Let's pin one.
283 launcherModel->pin("abs-icon");
284 while (appManager->count() > 0) {
285 appManager->removeApplication(0);
287 // The pinned one needs to stay, the other needs to disappear
288 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
289 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
292 void testGetUrlForAppId() {
293 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
294 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
295 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
296 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
297 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
298 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
301 void testIntrospection() {
302 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
303 QDBusReply<QString> reply = interface.call("Introspect");
304 QStringList nodes = extractNodes(reply.value());
305 QCOMPARE(nodes.count(), launcherModel->rowCount());
307 appManager->addApplication(new MockApp("foobar"));
308 reply = interface.call("Introspect");
309 nodes = extractNodes(reply.value());
310 QCOMPARE(nodes.contains("foobar"), true);
312 appManager->removeApplication(2);
313 reply = interface.call("Introspect");
314 nodes = extractNodes(reply.value());
315 QCOMPARE(nodes.contains("foobar"), false);
318 QStringList extractNodes(const QString &introspectionXml) {
319 QXmlStreamReader introspectReply(introspectionXml);
322 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
323 QXmlStreamReader::TokenType token = introspectReply.readNext();
325 if (token == QXmlStreamReader::StartElement) {
326 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
327 ret << introspectReply.attributes().value("name").toString();
334 void testCountEmblems() {
335 // Call GetAll on abs-icon
336 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
337 QDBusReply<QVariantMap> reply = interface.call("GetAll");
338 QVariantMap map = reply.value();
340 // Make sure GetAll returns a map with count and countVisible props
341 QCOMPARE(map.contains("count"), true);
342 QCOMPARE(map.contains("countVisible"), true);
344 // Make sure count is intitilized to 0 and non-visible
345 QCOMPARE(map.value("count").toInt(), 0);
346 QCOMPARE(map.value("countVisible").toBool(), false);
348 // Now make it visible and set it to 55 through D-Bus
349 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
350 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
352 // Fetch it again using GetAll
353 reply = interface.call("GetAll");
356 // Make sure values have changed on the D-Bus interface
357 QCOMPARE(map.value("count").toInt(), 55);
358 QCOMPARE(map.value("countVisible").toBool(), true);
360 // Now the item on the upper side of the API
361 int index = launcherModel->findApplication("abs-icon");
362 QCOMPARE(index >= 0, true);
364 // And make sure values have changed there as well
365 QCOMPARE(launcherModel->get(index)->countVisible(), true);
366 QCOMPARE(launcherModel->get(index)->count(), 55);
370 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
371 QDBusReply<void> reply = interface.call("Refresh");
373 // Make sure the call to Refresh returned without error.
374 QCOMPARE(reply.isValid(), true);
378QTEST_GUILESS_MAIN(LauncherModelTest)
379#include "launchermodeltest.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list