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) {}
110 QList<MockApp*> m_list;
113class LauncherModelTest : public QObject
118 LauncherModel *launcherModel;
119 MockAppManager *appManager;
123 void initTestCase() {
124 launcherModel = new LauncherModel(this);
125 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
127 appManager = new MockAppManager(this);
128 launcherModel->setApplicationManager(appManager);
131 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
133 appManager->addApplication(new MockApp("abs-icon"));
134 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
136 appManager->addApplication(new MockApp("no-icon"));
137 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
140 // Removing apps from appmanager and launcher as pinned ones would stick
142 while (appManager->count() > 0) {
143 appManager->removeApplication(0);
145 while (launcherModel->rowCount(QModelIndex()) > 0) {
146 launcherModel->requestRemove(launcherModel->get(0)->appId());
151 QCOMPARE(launcherModel->get(0)->pinned(), false);
152 QCOMPARE(launcherModel->get(1)->pinned(), false);
154 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
155 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
156 launcherModel->move(1, 0);
158 QCOMPARE(item0BeforeMove, launcherModel->get(1));
159 QCOMPARE(item1BeforeMove, launcherModel->get(0));
161 // moved item must be pinned now
162 QCOMPARE(item0BeforeMove->pinned(), false);
163 QCOMPARE(item1BeforeMove->pinned(), true);
167 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
168 QCOMPARE(launcherModel->get(0)->pinned(), false);
169 QCOMPARE(launcherModel->get(1)->pinned(), false);
170 launcherModel->pin(launcherModel->get(0)->appId());
171 QCOMPARE(launcherModel->get(0)->pinned(), true);
172 QCOMPARE(launcherModel->get(1)->pinned(), false);
173 QCOMPARE(spy.count(), 1);
174 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
176 launcherModel->requestRemove(launcherModel->get(0)->appId());
177 QCOMPARE(launcherModel->get(0)->pinned(), false);
178 QCOMPARE(launcherModel->get(1)->pinned(), false);
179 QCOMPARE(spy.count(), 2);
180 QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
183 void testRemove_data() {
185 QTest::addColumn<bool>("pinned");
186 QTest::addColumn<bool>("running");
188 QTest::newRow("non-pinned, running") << false << true;
189 QTest::newRow("pinned, running") << true << false;
190 QTest::newRow("pinned, non-running") << true << false;
194 QFETCH(bool, pinned);
195 QFETCH(bool, running);
197 // In the beginning we always have two items
198 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
200 // pin one if required
202 launcherModel->pin(launcherModel->get(1)->appId());
205 // stop it if required
207 appManager->removeApplication(1);
211 launcherModel->requestRemove(launcherModel->get(1)->appId());
214 // both apps are running, both apps must still be here
215 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
217 // Item must be unpinned now
218 QCOMPARE(launcherModel->get(1)->pinned(), false);
221 // Item 1 must go away, item 0 is here to stay
222 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
225 // done our checks. now stop the app if was still running
227 appManager->removeApplication(1);
230 // It needs to go away in any case now
231 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
234 void testQuickListPinningRemoving() {
235 // we start with 2 unpinned items
236 QCOMPARE(launcherModel->get(0)->pinned(), false);
237 QCOMPARE(launcherModel->get(1)->pinned(), false);
238 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
240 // find the Pin item in the quicklist
241 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
242 int pinActionIndex = -1;
243 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
244 if (model->get(i).actionId() == "pin_item") {
249 QVERIFY(pinActionIndex >= 0);
251 // trigger pin item quicklist action => Item must be pinned now.
252 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
253 QCOMPARE(launcherModel->get(0)->pinned(), true);
254 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
256 // quicklist needs to transform to remove item. trigger it and check it item goes away
257 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
258 QCOMPARE(launcherModel->get(0)->pinned(), false);
260 // still needs to be here as the app is still here
261 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
263 appManager->removeApplication(0);
264 // Now it needs to go away
265 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
268 void testApplicationFocused() {
269 // all apps unfocused at beginning...
270 QCOMPARE(launcherModel->get(0)->focused(), false);
271 QCOMPARE(launcherModel->get(1)->focused(), false);
273 appManager->focusApplication("abs-icon");
274 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
275 QCOMPARE(launcherModel->get(0)->focused(), true);
276 QCOMPARE(launcherModel->get(1)->focused(), false);
278 appManager->focusApplication("no-icon");
279 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
280 QCOMPARE(launcherModel->get(0)->focused(), false);
281 QCOMPARE(launcherModel->get(1)->focused(), true);
284 void testClosingApps() {
285 // At the start there are 2 items. Let's pin one.
286 launcherModel->pin("abs-icon");
287 while (appManager->count() > 0) {
288 appManager->removeApplication(0);
290 // The pinned one needs to stay, the other needs to disappear
291 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
292 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
295 void testGetUrlForAppId() {
296 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
297 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
298 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
299 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
300 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
301 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
304 void testIntrospection() {
305 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
306 QDBusReply<QString> reply = interface.call("Introspect");
307 QStringList nodes = extractNodes(reply.value());
308 QCOMPARE(nodes.count(), launcherModel->rowCount());
310 appManager->addApplication(new MockApp("foobar"));
311 reply = interface.call("Introspect");
312 nodes = extractNodes(reply.value());
313 QCOMPARE(nodes.contains("foobar"), true);
315 appManager->removeApplication(2);
316 reply = interface.call("Introspect");
317 nodes = extractNodes(reply.value());
318 QCOMPARE(nodes.contains("foobar"), false);
321 QStringList extractNodes(const QString &introspectionXml) {
322 QXmlStreamReader introspectReply(introspectionXml);
325 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
326 QXmlStreamReader::TokenType token = introspectReply.readNext();
328 if (token == QXmlStreamReader::StartElement) {
329 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
330 ret << introspectReply.attributes().value("name").toString();
337 void testCountEmblems() {
338 // Call GetAll on abs-icon
339 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
340 QDBusReply<QVariantMap> reply = interface.call("GetAll");
341 QVariantMap map = reply.value();
343 // Make sure GetAll returns a map with count and countVisible props
344 QCOMPARE(map.contains("count"), true);
345 QCOMPARE(map.contains("countVisible"), true);
347 // Make sure count is intitilized to 0 and non-visible
348 QCOMPARE(map.value("count").toInt(), 0);
349 QCOMPARE(map.value("countVisible").toBool(), false);
351 // Now make it visible and set it to 55 through D-Bus
352 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
353 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
355 // Fetch it again using GetAll
356 reply = interface.call("GetAll");
359 // Make sure values have changed on the D-Bus interface
360 QCOMPARE(map.value("count").toInt(), 55);
361 QCOMPARE(map.value("countVisible").toBool(), true);
363 // Now the item on the upper side of the API
364 int index = launcherModel->findApplication("abs-icon");
365 QCOMPARE(index >= 0, true);
367 // And make sure values have changed there as well
368 QCOMPARE(launcherModel->get(index)->countVisible(), true);
369 QCOMPARE(launcherModel->get(index)->count(), 55);
373 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
374 QDBusReply<void> reply = interface.call("Refresh");
376 // Make sure the call to Refresh returned without error.
377 QCOMPARE(reply.isValid(), true);
381QTEST_GUILESS_MAIN(LauncherModelTest)
382#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