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 QString splashTitle() const override { return QString(); }
47 QUrl splashImage() const override { return QUrl(); }
48 bool splashShowHeader() const override { return true; }
49 QColor splashColor() const override { return QColor(0,0,0,0); }
50 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
51 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
53 // Methods used for mocking (not in the interface)
54 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
60// This is a mock, specifically to test the LauncherModel
61class MockAppManager: public unity::shell::application::ApplicationManagerInterface
65 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
66 int rowCount(const QModelIndex &) const { return m_list.count(); }
67 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
68 QString focusedApplicationId() const {
69 Q_FOREACH(MockApp *app, m_list) {
70 if (app->focused()) return app->appId();
74 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
75 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
76 Q_FOREACH(MockApp* app, m_list) {
77 if (app->appId() == appId) {
83 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
84 bool stopApplication(const QString &) { return false; }
85 bool focusApplication(const QString &appId) {
86 Q_FOREACH(MockApp* app, m_list) {
87 app->setFocused(app->appId() == appId);
89 Q_EMIT focusedApplicationIdChanged();
93 void unfocusCurrentApplication() { }
95 void addApplication(MockApp *app) {
96 beginInsertRows(QModelIndex(), count(), count());
100 void removeApplication(int index) {
101 beginRemoveRows(QModelIndex(), index, index);
102 m_list.takeAt(index)->deleteLater();
105 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
106 bool suspended() const { return false; }
107 void setSuspended(bool) {}
108 bool forceDashActive() const { return false; }
109 void setForceDashActive(bool) {}
112 QList<MockApp*> m_list;
115class LauncherModelTest : public QObject
120 LauncherModel *launcherModel;
121 MockAppManager *appManager;
125 void initTestCase() {
126 launcherModel = new LauncherModel(this);
127 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
129 appManager = new MockAppManager(this);
130 launcherModel->setApplicationManager(appManager);
133 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
135 appManager->addApplication(new MockApp("abs-icon"));
136 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
138 appManager->addApplication(new MockApp("no-icon"));
139 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
142 // Removing apps from appmanager and launcher as pinned ones would stick
144 while (appManager->count() > 0) {
145 appManager->removeApplication(0);
147 while (launcherModel->rowCount(QModelIndex()) > 0) {
148 launcherModel->requestRemove(launcherModel->get(0)->appId());
153 QCOMPARE(launcherModel->get(0)->pinned(), false);
154 QCOMPARE(launcherModel->get(1)->pinned(), false);
156 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
157 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
158 launcherModel->move(1, 0);
160 QCOMPARE(item0BeforeMove, launcherModel->get(1));
161 QCOMPARE(item1BeforeMove, launcherModel->get(0));
163 // moved item must be pinned now
164 QCOMPARE(item0BeforeMove->pinned(), false);
165 QCOMPARE(item1BeforeMove->pinned(), true);
169 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
170 QCOMPARE(launcherModel->get(0)->pinned(), false);
171 QCOMPARE(launcherModel->get(1)->pinned(), false);
172 launcherModel->pin(launcherModel->get(0)->appId());
173 QCOMPARE(launcherModel->get(0)->pinned(), true);
174 QCOMPARE(launcherModel->get(1)->pinned(), false);
175 QCOMPARE(spy.count(), 1);
176 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
178 launcherModel->requestRemove(launcherModel->get(0)->appId());
179 QCOMPARE(launcherModel->get(0)->pinned(), false);
180 QCOMPARE(launcherModel->get(1)->pinned(), false);
181 QCOMPARE(spy.count(), 2);
182 QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
185 void testRemove_data() {
187 QTest::addColumn<bool>("pinned");
188 QTest::addColumn<bool>("running");
190 QTest::newRow("non-pinned, running") << false << true;
191 QTest::newRow("pinned, running") << true << false;
192 QTest::newRow("pinned, non-running") << true << false;
196 QFETCH(bool, pinned);
197 QFETCH(bool, running);
199 // In the beginning we always have two items
200 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
202 // pin one if required
204 launcherModel->pin(launcherModel->get(1)->appId());
207 // stop it if required
209 appManager->removeApplication(1);
213 launcherModel->requestRemove(launcherModel->get(1)->appId());
216 // both apps are running, both apps must still be here
217 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
219 // Item must be unpinned now
220 QCOMPARE(launcherModel->get(1)->pinned(), false);
223 // Item 1 must go away, item 0 is here to stay
224 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
227 // done our checks. now stop the app if was still running
229 appManager->removeApplication(1);
232 // It needs to go away in any case now
233 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
236 void testQuickListPinningRemoving() {
237 // we start with 2 unpinned items
238 QCOMPARE(launcherModel->get(0)->pinned(), false);
239 QCOMPARE(launcherModel->get(1)->pinned(), false);
240 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
242 // find the Pin item in the quicklist
243 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
244 int pinActionIndex = -1;
245 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
246 if (model->get(i).actionId() == "pin_item") {
251 QVERIFY(pinActionIndex >= 0);
253 // trigger pin item quicklist action => Item must be pinned now.
254 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
255 QCOMPARE(launcherModel->get(0)->pinned(), true);
256 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
258 // quicklist needs to transform to remove item. trigger it and check it item goes away
259 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
260 QCOMPARE(launcherModel->get(0)->pinned(), false);
262 // still needs to be here as the app is still here
263 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
265 appManager->removeApplication(0);
266 // Now it needs to go away
267 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
270 void testApplicationFocused() {
271 // all apps unfocused at beginning...
272 QCOMPARE(launcherModel->get(0)->focused(), false);
273 QCOMPARE(launcherModel->get(1)->focused(), false);
275 appManager->focusApplication("abs-icon");
276 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
277 QCOMPARE(launcherModel->get(0)->focused(), true);
278 QCOMPARE(launcherModel->get(1)->focused(), false);
280 appManager->focusApplication("no-icon");
281 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
282 QCOMPARE(launcherModel->get(0)->focused(), false);
283 QCOMPARE(launcherModel->get(1)->focused(), true);
286 void testClosingApps() {
287 // At the start there are 2 items. Let's pin one.
288 launcherModel->pin("abs-icon");
289 while (appManager->count() > 0) {
290 appManager->removeApplication(0);
292 // The pinned one needs to stay, the other needs to disappear
293 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
294 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
297 void testGetUrlForAppId() {
298 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
299 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
300 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
301 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
302 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
303 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
306 void testIntrospection() {
307 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
308 QDBusReply<QString> reply = interface.call("Introspect");
309 QStringList nodes = extractNodes(reply.value());
310 QCOMPARE(nodes.count(), launcherModel->rowCount());
312 appManager->addApplication(new MockApp("foobar"));
313 reply = interface.call("Introspect");
314 nodes = extractNodes(reply.value());
315 QCOMPARE(nodes.contains("foobar"), true);
317 appManager->removeApplication(2);
318 reply = interface.call("Introspect");
319 nodes = extractNodes(reply.value());
320 QCOMPARE(nodes.contains("foobar"), false);
323 QStringList extractNodes(const QString &introspectionXml) {
324 QXmlStreamReader introspectReply(introspectionXml);
327 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
328 QXmlStreamReader::TokenType token = introspectReply.readNext();
330 if (token == QXmlStreamReader::StartElement) {
331 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
332 ret << introspectReply.attributes().value("name").toString();
339 void testCountEmblems() {
340 // Call GetAll on abs-icon
341 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
342 QDBusReply<QVariantMap> reply = interface.call("GetAll");
343 QVariantMap map = reply.value();
345 // Make sure GetAll returns a map with count and countVisible props
346 QCOMPARE(map.contains("count"), true);
347 QCOMPARE(map.contains("countVisible"), true);
349 // Make sure count is intitilized to 0 and non-visible
350 QCOMPARE(map.value("count").toInt(), 0);
351 QCOMPARE(map.value("countVisible").toBool(), false);
353 // Now make it visible and set it to 55 through D-Bus
354 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
355 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
357 // Fetch it again using GetAll
358 reply = interface.call("GetAll");
361 // Make sure values have changed on the D-Bus interface
362 QCOMPARE(map.value("count").toInt(), 55);
363 QCOMPARE(map.value("countVisible").toBool(), true);
365 // Now the item on the upper side of the API
366 int index = launcherModel->findApplication("abs-icon");
367 QCOMPARE(index >= 0, true);
369 // And make sure values have changed there as well
370 QCOMPARE(launcherModel->get(index)->countVisible(), true);
371 QCOMPARE(launcherModel->get(index)->count(), 55);
375 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
376 QDBusReply<void> reply = interface.call("Refresh");
378 // Make sure the call to Refresh returned without error.
379 QCOMPARE(reply.isValid(), true);
383QTEST_GUILESS_MAIN(LauncherModelTest)
384#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